package tc.de.krawallmann.achievement;

import java.util.logging.Level;
import java.util.logging.Logger;

import tc.de.krawallmann.twl.widgets.MulticolorLabel;

/**
 * The Class AchievementNotifier notifies the user of unlocked Achievements.
 * @author KrawallMann
 * @version 1.0
 */
public class AchievementNotifier implements AchievementsListener {
	
	/** The Constant FADE_SPEED. */
	private static final float FADE_SPEED = 0.07f;

	/** The Constant SHOW_TIME. */
	private static final int SHOW_TIME = 2500;

	/** The last unlocked achievement. */
	private transient Achievement achievement;

	/** The label displaying achievement information. */
	private transient MulticolorLabel achievementLabel;

	/** The faded in state. */
	private transient long fadedIn;

	/** The fading in state. */
	private transient boolean fadingIn;

	/** The y position as double. */
	private transient double realY;

	/* (non-Javadoc)
	 * @see tc.de.krawallmann.achievement.AchievementsListener#achievementUnlocked(tc.de.krawallmann.achievement.Achievement)
	 */
	@Override
	public void achievementUnlocked(final Achievement achievement) {
		Logger.getLogger("").log(Level.INFO, "Unlocked achievement: "+achievement.getInternalString());
		this.achievement = achievement;
		fadingIn = true;
	}

	/**
	 * Creates the achievement label.
	 * 
	 * @return the label
	 */
	public MulticolorLabel createAchievementLabel() {
		achievementLabel = new MulticolorLabel("");
		achievementLabel.setTheme("/label40");
		achievementLabel.setColor(MulticolorLabel.Color.GREEN);
		achievementLabel.adjustSize();
		achievementLabel.setVisible(false);
		achievementLabel.setFocusKeyEnabled(false);
		return achievementLabel;
	}

	/**
	 * Gets the achievement label.
	 * 
	 * @return the achievement label
	 */
	public MulticolorLabel getAchievementLabel() {
		return achievementLabel;
	}

	/**
	 * Layouts the label and its' position.
	 * 
	 * @param containerWidth
	 *            the container width
	 */
	public void layout(final int containerWidth) {
		achievementLabel.adjustSize();
		achievementLabel.setPosition((containerWidth - achievementLabel.getWidth()) / 2,
				-achievementLabel.getHeight());
		realY = achievementLabel.getY();
	}

	/**
	 * Update.
	 * 
	 * @param delta
	 *            the delta
	 */
	public void update(final long delta) {
		if (achievement != null) {
			achievementLabel.setText(achievement.getName() + " - " + achievement.getPoints() + "P");
		}
		if (fadingIn) {
			achievementLabel.setVisible(true);
			if (achievementLabel.getY() < 0) {
				realY += AchievementNotifier.FADE_SPEED * delta;
				achievementLabel.setPosition(achievementLabel.getX(), (int) (Math.round(realY)));
			} else {
				fadedIn = System.currentTimeMillis();
				fadingIn = false;
				Logger.getLogger("").log(Level.INFO, "Achievement label faded in");
			}
		} else {
			if ((fadedIn != -1)
					&& (fadedIn + AchievementNotifier.SHOW_TIME < System.currentTimeMillis())) {
				if (achievementLabel.getY() > -achievementLabel.getHeight()) {
					realY -= AchievementNotifier.FADE_SPEED * delta;
					achievementLabel
							.setPosition(achievementLabel.getX(), (int) (Math.round(realY)));
				} else {
					Logger.getLogger("").log(Level.INFO, "Achievement label faded out");
					fadedIn = -1;
					achievementLabel.setVisible(false);
				}
			}
		}
	}
}
